import requests
import json
from bs4 import BeautifulSoup



def get_html(url,headers):

   r = requests.get(url,headers=headers)

   r.encoding = r.apparent_encoding

   return r.text



def get_pages(html):

   soup = BeautifulSoup(html,'html.parser')

   all_topics=soup.find_all('div',class_='category-wrap_iQLoo')

   all_json={}
   i = 0
#    jsons = json.dumps(all_json)

   for each_topic in all_topics:

       topic_times = each_topic.find('div', class_='hot-index_1Bl1a')  # 搜索指数

       topic_rank = each_topic.find('div', class_='index_1Ew5p')  # 排名

       topic_name = each_topic.find('div', class_='c-single-text-ellipsis')  # 标题目

       topic_tag  = each_topic.find('div',class_='hot-tag_1G080')  #标志

       topic_link = each_topic.find('a') # 链接
       if topic_rank != None and topic_name != None and topic_times != None and topic_tag != None:

           topic_rank = each_topic.find('div', class_='index_1Ew5p').get_text().replace(' ', '').replace('\n', '')

           topic_name = each_topic.find('div', class_='c-single-text-ellipsis').get_text().replace(' ', '').replace('\n', '')

           topic_times = each_topic.find('div', class_='hot-index_1Bl1a').get_text().replace(' ', '').replace('\n', '')

           topic_tag = each_topic.find('div', class_='hot-tag_1G080').get_text().replace(' ', '').replace('\n', '')

           topic_link = each_topic.find('a').get_text()

           # print('排名：{}，标题：{}，热度：{}'.format(topic_rank,topic_name,topic_times))

#            tplt = "排名：{0:^25}\t标题：{1:{3}^15}\t热度：{2:^9} \t标志:{3:^5}"
           dd = {"rank":topic_rank,"title":topic_name,"tag":topic_tag,"times":topic_times,"link":topic_link}
           all_json[i]=dd
#            all_json[i]=dd
           i= i + 1
#            print(tplt.format(topic_rank, topic_name,topic_times,topic_tag,chr(12288)))
#            jsons.append(dd)

   return all_json


def main():

    #百度热点排行榜单链接

    url = 'https://top.baidu.com/board?tab=realtime'

    headers = {'User-Agent': 'Mozilla/5.0'}

    html = get_html(url, headers)

    jsons= get_pages(html)
#     print(json.dumps(jsons))
    with open("./record.json","w") as f:
        json.dump(jsons,f)
    print("写入文件完成...")

if __name__ == '__main__':

    main()
